﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DLM.Domain.Model;
using DLM.Services.Interfaces;
using DLM.Web.MVC.Extensions;
using DLM.Web.ViewModel;
using System.Collections.Specialized;

namespace DLM.Web.MVC.Controllers
{

    public class PaymentController : Controller
    {

        private DLM.Services.Interfaces.IPaymentService paymentService;
        public PaymentController(DLM.Services.Interfaces.IPaymentService paymentService)
        {
            this.paymentService = paymentService;

        }

   
        public ActionResult Orders(int? pageindex)
        {

            var list = this.paymentService.MyOrders(User.Identity.Name, pageindex ?? 0, 15);
            return View(list);
        }

        [UserAuthorize]
        public ActionResult Detail(string orderNo)
        {
            var order = this.paymentService.DetailOrder(orderNo);
            return View(order);
        }

        [HttpGet]
        [UserAuthorize]
        public ActionResult Buy()
        {
            return View();
        }

        [HttpPost]
        [UserAuthorize]
        public void Buy(BuyPageView item)
        {
            string out_trade_no = DateTime.Now.ToString("yyyyMMddHHmmss");
            string subject = item.Subject;
            string body = item.Body;
            string price = item.Price;
            SortedDictionary<string, string> sParaTemp = new SortedDictionary<string, string>();
            sParaTemp.Add("body", body);
            sParaTemp.Add("out_trade_no", out_trade_no);
            sParaTemp.Add("price", price);
            sParaTemp.Add("subject", subject);
            string html = this.paymentService.PayForOrder(sParaTemp);
            Response.Write(html);
        }

        [HttpGet]
        public ActionResult Return()
        {
            var returnPara = GetRequestGet();
            this.paymentService.ProcessReturnUrl(returnPara);
            return RedirectToAction("Orders");
        }

        [HttpPost]
        public void Notify()
        {
            var notifyPara = GetRequestPost();
            bool result = this.paymentService.ProcessNotify(notifyPara);
            if (result)
            {
                Response.Write("success");
            } 
            
        }

        [HttpPost]
        [UserAuthorize]
        public void Confirm(string orderNo,string tradeNo)
        {
            Order order = new Order 
            {
                 OrderNo = orderNo,
                 TradeNo = tradeNo
            };
            var response = this.paymentService.ConfirmForOrder(order);
             Response.Write(response);
            //return RedirectToAction("Detail", new { orderNo = orderNo });
        }

        private SortedDictionary<string, string> GetRequestGet()
        {
            int i = 0;
            SortedDictionary<string, string> sArray = new SortedDictionary<string, string>();
            NameValueCollection coll;
            //Load Form variables into NameValueCollection variable.
            coll = Request.QueryString;

            // Get names of all forms into a string array.
            String[] requestItem = coll.AllKeys;

            for (i = 0; i < requestItem.Length; i++)
            {
                sArray.Add(requestItem[i], Request.QueryString[requestItem[i]]);
            }

            return sArray;
        }

        /// <summary>
        /// 获取支付宝POST过来通知消息，并以“参数名=参数值”的形式组成数组
        /// </summary>
        /// <returns>request回来的信息组成的数组</returns>
        public SortedDictionary<string, string> GetRequestPost()
        {
            int i = 0;
            SortedDictionary<string, string> sArray = new SortedDictionary<string, string>();
            NameValueCollection coll;
            //Load Form variables into NameValueCollection variable.
            coll = Request.Form;

            // Get names of all forms into a string array.
            String[] requestItem = coll.AllKeys;

            for (i = 0; i < requestItem.Length; i++)
            {
                sArray.Add(requestItem[i], Request.Form[requestItem[i]]);
            }

            return sArray;
        } 

    }
}
